/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HraKlient.java
 *
 * Created on 15.11.2011, 16:54:47
 */
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 *
 * @author Riso
 */
public class HraKlient extends Hra {

    Pozadie nove = new Pozadie(0,0);
    static String IP;
    Socket klient= null;
    
    
    /** Creates new form HraKlient */

    public HraKlient(String IP) {
        this.IP=IP;
        initComponents();
        
        setCursor(new Cursor(Cursor.WAIT_CURSOR));
        try {
            InetAddress adresa = InetAddress.getByName(IP); // to iste ako localhost
            klient = new Socket(adresa, port);
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(klient.getOutputStream())), true);
            in = new BufferedReader(new InputStreamReader(klient.getInputStream()));
            th.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

    }

    public synchronized void nacitajPozadie(String s) {
        int pocitadlo = 0;
        for (int i = 0; i < 200; i++) {
            for (int k = 0; k < 200; k++) {
                p.pole[i][k] = Integer.parseInt(s.substring(pocitadlo, pocitadlo + 1));
                pocitadlo++;
            }
        }
        repaint();
    }
    @Override
    public void naraz() {
        int x = udajemoje[strelax] / 5;
        int y = udajemoje[strelay] / 5;
        if(x>197||x<3||y>197||y<3){
            udajemoje[strelax] = 0;
            udajemoje[strelay] = 0;
            str.naraz = true;
            str = null;
        }else if (p.pole[x][y] != 3) {
            udajemoje[narazje] = 1;
            str.naraz = true;
            str = null;
        }
    }

    @Override
    public void nacitajKoniec() {
        try {
            int ano= Integer.parseInt(in.readLine());
            if(ano==1){
            konec=true;
            s.close();
            }
        } catch (Exception ex) {
        }
    }

    

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Klient");
        setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void run() {
        try {
        while (bez) {
            aktualizujUdaje();
            doplnenieVydrze();
                if (str != null) {
                    naraz();
                }
                nacitajPozadie(in.readLine());
                odosliUdaje();
                nacitajUdaje();
                nacitajKoniec();
                odosliKoniec();
                ukazEnemy();
                
                if (p.getX() > getWidth() / 2 - 13) {
            p.setLocation(p.getX() - 1, p.getY());
            z.setLocation(z.getX() - 1, z.getY());
            zakladnaEnemy.setLocation(zakladnaEnemy.getX() - 1, zakladnaEnemy.getY());
        }
        if (p.getX() > getWidth() / 2 - 13) {
                    p.setLocation(p.getX() - 1, p.getY());
                    z.setLocation(z.getX() - 1, z.getY());
                    zakladnaEnemy.setLocation(zakladnaEnemy.getX() - 1, zakladnaEnemy.getY());
                }
                if (p.getY() > getHeight() / 2 - 16) {
                    p.setLocation(p.getX(), p.getY() - 1);
                    z.setLocation(z.getX(), z.getY() - 1);
                    zakladnaEnemy.setLocation(zakladnaEnemy.getX(), zakladnaEnemy.getY() - 1);
                }
                if (p.getX() < getWidth() / 2 + 14 - 999) {
                    p.setLocation(p.getX() + 1, p.getY());
                    z.setLocation(z.getX() + 1, z.getY());
                    zakladnaEnemy.setLocation(zakladnaEnemy.getX() + 1, zakladnaEnemy.getY());
                }
                if (p.getY() < getHeight() / 2 + 14 - 999) {
                    p.setLocation(p.getX(), p.getY() + 1);
                    z.setLocation(z.getX(), z.getY() + 1);
                    zakladnaEnemy.setLocation(zakladnaEnemy.getX(), zakladnaEnemy.getY() + 1);
                }
                //doprava
                if (keys.contains(new Integer(68))) {

                    t.smer = 1;
                    if (p.pole[(-p.getX()+13 + getWidth() / 2) / 5][(-p.getY() + getHeight() / 2) / 5] != 3 & vydrz >= 1) {
                        //p.zaznacit(-p.getX() + getWidth() / 2, -p.getY() + getHeight() / 2);
                        vydrz--;
                        p.setLocation(p.getX() - 1, p.getY());
                        z.setLocation(z.getX() - 1, z.getY());
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX() - 1, zakladnaEnemy.getY());
                        if (str != null) {
                            str.setLocation(str.getX() - 1, str.getY());
                        }
                    } else if (p.pole[(-p.getX()+13 + getWidth() / 2) / 5][(-p.getY() + getHeight() / 2) / 5] == 3) {
                        //p.zaznacit(-p.getX() + getWidth() / 2, -p.getY() + getHeight() / 2);
                        p.setLocation(p.getX() - 1, p.getY());
                        z.setLocation(z.getX() - 1, z.getY());
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX() - 1, zakladnaEnemy.getY());
                        if (str != null) {
                            str.setLocation(str.getX() - 1, str.getY());
                        }
                    }
                }
                //dolava
                if (keys.contains(new Integer(65))) {

                    t.smer = 2;
                    if (p.pole[(-p.getX()-13 + getWidth() / 2) / 5][(-p.getY() + getHeight() / 2) / 5] != 3 & vydrz >= 1) {
                        //p.zaznacit(-p.getX() + getWidth() / 2, -p.getY() + getHeight() / 2);
                        vydrz--;
                        p.setLocation(p.getX() + 1, p.getY());
                        z.setLocation(z.getX() + 1, z.getY());
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX() + 1, zakladnaEnemy.getY());
                        if (str != null) {
                            str.setLocation(str.getX() + 1, str.getY());
                        }
                    } else if (p.pole[(-p.getX()-13 + getWidth() / 2) / 5][(-p.getY() + getHeight() / 2) / 5] == 3) {
                        //p.zaznacit(-p.getX() + getWidth() / 2, -p.getY() + getHeight() / 2);
                        p.setLocation(p.getX() + 1, p.getY());
                        z.setLocation(z.getX() + 1, z.getY());
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX() + 1, zakladnaEnemy.getY());
                        if (str != null) {
                            str.setLocation(str.getX() + 1, str.getY());
                        }
                    }
                }
                //hore
                if (keys.contains(new Integer(87))) {

                    t.smer = 4;
                    if (p.pole[(-p.getX() + getWidth() / 2) / 5][(-p.getY()-13 + getHeight() / 2) / 5] != 3 & vydrz >= 1) {
                        //p.zaznacit(-p.getX() + getWidth() / 2, -p.getY() + getHeight() / 2);
                        vydrz--;
                        p.setLocation(p.getX(), p.getY() + 1);
                        z.setLocation(z.getX(), z.getY() + 1);
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX(), zakladnaEnemy.getY() + 1);
                        if (str != null) {
                            str.setLocation(str.getX(), str.getY() + 1);
                        }
                    } else if (p.pole[(-p.getX() + getWidth() / 2) / 5][(-p.getY()-13 + getHeight() / 2) / 5] == 3) {
                        //p.zaznacit(-p.getX() + getWidth() / 2, -p.getY() + getHeight() / 2);
                        p.setLocation(p.getX(), p.getY() + 1);
                        z.setLocation(z.getX(), z.getY() + 1);
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX(), zakladnaEnemy.getY() + 1);
                        if (str != null) {
                            str.setLocation(str.getX(), str.getY() + 1);
                        }
                    }
                }
                //dole
                if (keys.contains(new Integer(83))) {

                    t.smer = 3;
                    if (p.pole[(-p.getX() + getWidth() / 2) / 5][(-p.getY()+13 + getHeight() / 2) / 5] != 3 & vydrz >= 1) {
                        //p.zaznacit(-(p.getX()) + getWidth() / 2, -(p.getY()) + getHeight() / 2);
                        vydrz--;
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX(), zakladnaEnemy.getY() - 1);
                        p.setLocation(p.getX(), p.getY() - 1);
                        z.setLocation(z.getX(), z.getY() - 1);
                        if (str != null) {
                            str.setLocation(str.getX(), str.getY() - 1);
                        }
                    } else if (p.pole[(-p.getX() + getWidth() / 2) / 5][(-p.getY()+13 + getHeight() / 2) / 5] == 3) {
                        //p.zaznacit(-(p.getX()) + getWidth() / 2, -(p.getY()) + getHeight() / 2);
                        zakladnaEnemy.setLocation(zakladnaEnemy.getX(), zakladnaEnemy.getY() - 1);
                        p.setLocation(p.getX(), p.getY() - 1);
                        z.setLocation(z.getX(), z.getY() - 1);
                        if (str != null) {
                            str.setLocation(str.getX(), str.getY() - 1);
                        }
                    }
                }
                th.sleep(20);
                if(konec){
                    bez=false;
                    dispose();
                }
            }
            } catch (Exception ex) {
            }finally{
            try {
                socket.close();
                s.close();
            } catch (Exception e) {
            }
        }
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HraKlient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HraKlient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HraKlient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HraKlient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new HraKlient(IP).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
